<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="值类型转换var a &#x3D; 42var b &#x3D; a+&amp;#x27;&amp;#x27;      &#x2F;&#x2F;隐式强制类型转换var c &#x3D; String(a) &#x2F;&#x2F;显示强制类型转换   抽象值操作toString(..)负责处理非字符串到字符串的强制类型转换 var a &#x3D; 10000000000000000*10000000000000000;console.log(a.toString()) &#x2F;&#x2F;1e+32">
<meta property="og:type" content="article">
<meta property="og:title" content="强制类型转换">
<meta property="og:url" content="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:description" content="值类型转换var a &#x3D; 42var b &#x3D; a+&amp;#x27;&amp;#x27;      &#x2F;&#x2F;隐式强制类型转换var c &#x3D; String(a) &#x2F;&#x2F;显示强制类型转换   抽象值操作toString(..)负责处理非字符串到字符串的强制类型转换 var a &#x3D; 10000000000000000*10000000000000000;console.log(a.toString()) &#x2F;&#x2F;1e+32">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-04-19T07:21:08.000Z">
<meta property="article:modified_time" content="2021-05-07T01:33:24.604Z">
<meta property="article:author" content="Lawted Wu">
<meta property="article:tag" content="JS U dont know 2">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>
<title>强制类型转换 | Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%80%BC%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">1.</span> <span class="nav-text">值类型转换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8A%BD%E8%B1%A1%E5%80%BC%E6%93%8D%E4%BD%9C"><span class="nav-number">2.</span> <span class="nav-text">抽象值操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#toString-NaN"><span class="nav-number">2.0.1.</span> <span class="nav-text">toString(..)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#JSON%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%96"><span class="nav-number">2.0.2.</span> <span class="nav-text">JSON字符串化</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#toNumber"><span class="nav-number">2.1.</span> <span class="nav-text">toNumber(..)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%98%BE%E5%BC%8F%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">3.</span> <span class="nav-text">显式强制类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E6%95%B0%E5%AD%97%E4%B9%8B%E9%97%B4%E7%9A%84%E6%98%BE%E5%BC%8F%E8%BD%AC%E6%8D%A2"><span class="nav-number">3.1.</span> <span class="nav-text">字符串和数字之间的显式转换</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%97%A5%E6%9C%9F%E6%98%BE%E5%BC%8F%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E5%AD%97"><span class="nav-number">3.1.1.</span> <span class="nav-text">日期显式转换为数字</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E5%BC%8F%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%B8%83%E5%B0%94%E5%80%BC"><span class="nav-number">3.1.2.</span> <span class="nav-text">显式转换为布尔值</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="nav-number">3.2.</span> <span class="nav-text">~操作符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9A%90%E5%BC%8F%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">4.</span> <span class="nav-text">隐式强制类型转换</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E6%95%B0%E5%AD%97%E4%B9%8B%E9%97%B4%E7%9A%84%E9%9A%90%E5%BC%8F%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">4.0.1.</span> <span class="nav-text">字符串和数字之间的隐式强制类型转换</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9A%90%E5%BC%8F%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%88%B0%E5%B8%83%E5%B0%94%E5%80%BC"><span class="nav-number">4.1.</span> <span class="nav-text">隐式强制类型转换到布尔值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%92%8C-amp-amp"><span class="nav-number">4.2.</span> <span class="nav-text">|| 和 &amp;&amp;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#null%E5%92%8Cundefined%E4%B9%8B%E9%97%B4%E7%9A%84%E6%AF%94%E8%BE%83"><span class="nav-number">4.3.</span> <span class="nav-text">null和undefined之间的比较</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          强制类型转换
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-19 15:21:08" itemprop="dateCreated datePublished" datetime="2021-04-19T15:21:08+08:00">2021-04-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:33:24" itemprop="dateModified" datetime="2021-05-07T09:33:24+08:00">2021-05-07</time>
      </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="值类型转换"><a href="#值类型转换" class="headerlink" title="值类型转换"></a>值类型转换</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> b = a+<span class="string">&#x27;&#x27;</span>      <span class="comment">//隐式强制类型转换</span></span><br><span class="line"><span class="keyword">var</span> c = <span class="built_in">String</span>(a) <span class="comment">//显示强制类型转换</span></span><br></pre></td></tr></table></figure>


<h2 id="抽象值操作"><a href="#抽象值操作" class="headerlink" title="抽象值操作"></a>抽象值操作</h2><h4 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString(..)"></a>toString(..)</h4><p>负责处理非字符串到字符串的强制类型转换</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">10000000000000000</span>*<span class="number">10000000000000000</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a.toString()) <span class="comment">//1e+32</span></span><br></pre></td></tr></table></figure>


<h4 id="JSON字符串化"><a href="#JSON字符串化" class="headerlink" title="JSON字符串化"></a>JSON字符串化</h4><p><code>JSON.stringify</code>将JSON对象序列化为字符串时也用到了<code>toString(..)</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">JSON</span>.stringify(<span class="literal">true</span>)  <span class="comment">//&quot;true&quot;</span></span><br></pre></td></tr></table></figure>


<h3 id="toNumber"><a href="#toNumber" class="headerlink" title="toNumber(..)"></a>toNumber(..)</h3><p>将非数字值当作数字来使用，<code>true</code>转换为<code>1</code>，<code>false</code>转换为<code>0</code>,<code>undefined</code>转换为<code>NaN</code>,<code>null</code>转换为<code>0</code></p>
<h2 id="显式强制类型转换"><a href="#显式强制类型转换" class="headerlink" title="显式强制类型转换"></a>显式强制类型转换</h2><h3 id="字符串和数字之间的显式转换"><a href="#字符串和数字之间的显式转换" class="headerlink" title="字符串和数字之间的显式转换"></a>字符串和数字之间的显式转换</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> b = <span class="built_in">String</span>(a)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = <span class="string">&quot;3.14&quot;</span></span><br><span class="line"><span class="keyword">var</span> d = <span class="built_in">Number</span>(c)</span><br><span class="line"><span class="keyword">var</span> e = +c</span><br></pre></td></tr></table></figure>


<h4 id="日期显式转换为数字"><a href="#日期显式转换为数字" class="headerlink" title="日期显式转换为数字"></a>日期显式转换为数字</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> d = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">&quot;Mon, 19 Apr 2021 16:15:33 CDT&quot;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(+d)</span><br><span class="line"><span class="comment">//下面的方式同样可以获取时间戳</span></span><br><span class="line"><span class="keyword">var</span> timestamp1 = +<span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line"><span class="keyword">var</span> timestamp2 = <span class="built_in">Date</span>.now()</span><br></pre></td></tr></table></figure>


<h4 id="显式转换为布尔值"><a href="#显式转换为布尔值" class="headerlink" title="显式转换为布尔值"></a>显式转换为布尔值</h4><p>显式强制类型转换为布尔值最常用的方法是<code>!!</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;0&quot;</span></span><br><span class="line"><span class="keyword">var</span> b = []</span><br><span class="line"><span class="keyword">var</span> c = &#123;&#125;</span><br><span class="line"><span class="built_in">console</span>.log(!!a)</span><br><span class="line"><span class="built_in">console</span>.log(!!b)</span><br><span class="line"><span class="built_in">console</span>.log(!!c)</span><br></pre></td></tr></table></figure>


<h3 id="操作符"><a href="#操作符" class="headerlink" title="~操作符"></a><code>~</code>操作符</h3><p>在for循环中，可以使用<code>~</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">10</span> ;~i;i--)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>相比于<code>i&gt;-1</code>或者<code>i&gt;=0</code>来说<code>~i</code>更加优雅，注意<code>~-1==0</code>,<code>~0==-1</code>,<code>~1==-2</code></p>
<h2 id="隐式强制类型转换"><a href="#隐式强制类型转换" class="headerlink" title="隐式强制类型转换"></a>隐式强制类型转换</h2><h4 id="字符串和数字之间的隐式强制类型转换"><a href="#字符串和数字之间的隐式强制类型转换" class="headerlink" title="字符串和数字之间的隐式强制类型转换"></a>字符串和数字之间的隐式强制类型转换</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;42&quot;</span></span><br><span class="line"><span class="keyword">var</span> b = <span class="string">&quot;0&quot;</span></span><br><span class="line"><span class="keyword">var</span> c = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> d = <span class="number">0</span></span><br><span class="line"><span class="keyword">var</span> e = [<span class="number">1</span>,<span class="number">2</span>]</span><br><span class="line"><span class="keyword">var</span> f = [<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a+b) <span class="comment">//&#x27;420&#x27;,字符串拼接</span></span><br><span class="line"><span class="built_in">console</span>.log(c+d) <span class="comment">//42，数字加法</span></span><br><span class="line"><span class="built_in">console</span>.log(e+f) <span class="comment">//1,23,4，转换成字符串拼接</span></span><br></pre></td></tr></table></figure>


<h3 id="隐式强制类型转换到布尔值"><a href="#隐式强制类型转换到布尔值" class="headerlink" title="隐式强制类型转换到布尔值"></a>隐式强制类型转换到布尔值</h3><ul>
<li><code>if(..)</code>语句中的条件判断语句</li>
<li><code>for(..;..;..)</code>语句的条件判断表达式</li>
<li><code>while(..)</code>和<code>do..while(..)</code>循环中的条件判断表达式</li>
<li>三元判断</li>
<li>逻辑运算符 <code>||</code>和<code>&amp;&amp;</code>左边的操作数</li>
</ul>
<h3 id="和-amp-amp"><a href="#和-amp-amp" class="headerlink" title="|| 和 &amp;&amp;"></a>|| 和 &amp;&amp;</h3><p>逻辑运算符和C中不一样</p>
<p>首先判断第一个的结果</p>
<p>对于<code>||</code>来说，如果条件判断结果是<code>true</code>会返回第一个操作数的值，如果为<code>false</code>则返回第二个操作数的值</p>
<p>对于<code>&amp;&amp;</code>来说，如果条件判断结果是<code>true</code>会返回第二个操作数的值，如果为<code>false</code>则返回第一个操作数的值</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> || <span class="number">0</span>) <span class="comment">//4,4转换为true然后因为是或运算，直接返回第一个值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">0</span> || <span class="number">4</span>) <span class="comment">//4,0转换为false,或运算再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> &amp;&amp; <span class="number">0</span>) <span class="comment">//0,4转换为true,与运算再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">0</span> &amp;&amp; <span class="number">4</span>) <span class="comment">//0,0转换为false,与运算不看第二个直接返回第一个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">5</span> &amp;&amp; <span class="number">4</span>) <span class="comment">//4,5转换为true,再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> &amp;&amp; <span class="number">5</span>) <span class="comment">//5,4转换为true,再看看第二个的值</span></span><br></pre></td></tr></table></figure>
<p>类似于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">a || b</span><br><span class="line">a ? a : b</span><br><span class="line"></span><br><span class="line">a &amp;&amp; b</span><br><span class="line">a ? b : a</span><br></pre></td></tr></table></figure>


<p>一个非常常见的用法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">say</span> (<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">    a = a||<span class="string">&quot;hello&quot;</span></span><br><span class="line">    b = b||<span class="string">&quot;world&quot;</span></span><br><span class="line">    <span class="built_in">console</span>.log(a+ <span class="string">&quot; &quot;</span> +b)</span><br><span class="line">&#125;</span><br><span class="line">say()           <span class="comment">//hello world</span></span><br><span class="line">say(<span class="string">&quot;ok&quot;</span>,<span class="string">&quot;hey&quot;</span>) <span class="comment">//ok hey</span></span><br></pre></td></tr></table></figure>
<p>如果<code>a</code>没有值，那么就默认是<code>hello</code>，因为这个是或运算，第一个转换为<code>false</code>后就看第二个是什么来决定最终的值</p>
<p>一个不是很常见的压缩方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;running foo&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">4</span></span><br><span class="line">a &amp;&amp; foo()</span><br><span class="line"></span><br><span class="line"><span class="comment">//一般我们这样写</span></span><br><span class="line"><span class="keyword">if</span>(a)</span><br><span class="line">foo()</span><br></pre></td></tr></table></figure>
<p><code>foo()</code>只有再条件判断<code>a</code>通过的时候才会被调用，如果条件判断未通过，就不会执行<code>foo()</code></p>
<p>注意： <code>&amp;&amp;</code>的优先级比<code>||</code>的高</p>
<h3 id="null和undefined之间的比较"><a href="#null和undefined之间的比较" class="headerlink" title="null和undefined之间的比较"></a>null和undefined之间的比较</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="literal">null</span> == <span class="literal">undefined</span>) <span class="comment">//true </span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">undefined</span> == <span class="literal">null</span>) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JS-U-dont-know-2/" rel="tag"># JS U dont know 2</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%8E%9F%E7%94%9F%E5%87%BD%E6%95%B0/" rel="prev" title="原生函数">
                  <i class="fa fa-chevron-left"></i> 原生函数
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/04/20/JS%20U%20DONT%20KNOW%202/%E7%9F%AD%E8%B7%AF/" rel="next" title="短路">
                  短路 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
